import 'package:backend.core/core/domain/entities/api/google_play/api_google_play_one_time_purchased_item_transaction.dart';
import 'package:equatable/equatable.dart';
import 'package:backend.purchases/infrastructure/dtos/api/api_time_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/google_play/api_google_play_purchased_item_transaction_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/api/google_play/api_google_play_transaction_revocation_info_dto.dart';

const kApiGooglePlayOneTimePurchasedItemTransactionPromotionalKey = 'promotional';

class ApiGooglePlayOneTimePurchasedItemTransactionDto extends Equatable
    implements ApiGooglePlayPurchasedItemTransactionDto {
  const ApiGooglePlayOneTimePurchasedItemTransactionDto._internal({
    required this.orderId,
    required this.timeDto,
    required this.promotional,
    required this.revocationInfoDto,
  });

  @override
  final String orderId;

  @override
  final ApiTimeDto timeDto;

  @override
  final ApiGooglePlayTransactionRevocationInfoDto? revocationInfoDto;

  final bool promotional;

  @override
  List<Object?> get props {
    return [
      orderId,
      timeDto,
      promotional,
      revocationInfoDto,
    ];
  }

  factory ApiGooglePlayOneTimePurchasedItemTransactionDto.fromEntity(
      ApiGooglePlayOneTimePurchasedItemTransaction transaction) {
    ApiGooglePlayTransactionRevocationInfoDto? revocationInfoDto;

    if (transaction.revocationInfo != null) {
      revocationInfoDto =
          ApiGooglePlayTransactionRevocationInfoDto.fromEntity(transaction.revocationInfo!);
    }

    final timeDto = ApiTimeDto.fromEntity(transaction.time);

    return ApiGooglePlayOneTimePurchasedItemTransactionDto._internal(
      orderId: transaction.orderId,
      timeDto: timeDto,
      promotional: transaction.promotional,
      revocationInfoDto: revocationInfoDto,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      kApiGooglePlayPurchasedItemTransactionOrderIdKey: orderId,
      kApiGooglePlayPurchasedItemTransactionTimeKey: timeDto.toString(),
      kApiGooglePlayOneTimePurchasedItemTransactionPromotionalKey: promotional,
      kApiGooglePlayPurchasedItemTransactionRevocationInfoKey: revocationInfoDto?.toJson(),
    };
  }
}
